﻿using AIVision.Inspect.Camera;
using AIVision.InspectModule.Inspect.HCDL;
using AIVision.InspectModule.Inspect.Models;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace AIVision.InspectModule.Inspect
{
    public class InspectAgent
    {
        private readonly InspectCfgModel inspectCfg;
        public ICheckUnit checkUnit;

        //private HObject currentImage;
        //public HObject CurrentImage { get => currentImage; set => currentImage = value; }
        //private InspectResultModel inspectResult;
        //public InspectResultModel InspectResult { get => inspectResult; set => inspectResult = value; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inspectCfg">检测配置</param>
        /// <param name="defectItemModels">不良项配置</param>
        public InspectAgent(InspectCfgModel inspectCfg)
        {
            this.inspectCfg = inspectCfg;
        }

        //public void Init()
        //{
        //    OpenCamera();
        //    InitInspectModule();
        //}

        //public bool TriggerPhoto(ref string ErrMsg)
        //{
        //    try
        //    {
        //        //if (CurrentImage != null)
        //        //{
        //        //    CurrentImage.Dispose();
        //        //    CurrentImage = null;
        //        //}
        //       HObject CurrentImage = camera.OneGrap();
        //        if (CurrentImage != null)
        //            return true;
        //        else
        //        {
        //            ErrMsg = "获取图像为空";
        //            return false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        ErrMsg = ex.Message;
        //        return false;
        //    }

        //}
        public InspectResultModel RunInspect(string SourcePhotoPath)
        {
            try
            {
                HObject currentImage = new HObject();
                HOperatorSet.ReadImage(out currentImage, SourcePhotoPath);
                var  result = checkUnit.ExUnitCheck(currentImage);

                return result;
            }
            catch (Exception ex)
            {
                return new InspectResultModel() { RunResult=false,Message=ex.Message};
            }
        }


        //private void OpenCamera()
        //{
        //    try
        //    {
        //        camera = new HKCamerSDK();//根据我们需要开启的相机品牌去实例化化相机类
        //        camera.CamerName = inspectCfgModel.CameraName;
        //        var list = camera.FindCamer();
        //        var v = camera.OpenCamer(inspectCfgModel.CameraHardKey);
        //        if (!v)
        //        {
        //            throw new Exception($"相机{camera.CamerName}打开失败");
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception($"相机{camera.CamerName}打开失败");
        //    }
        //}
        public void Init()
        {
            checkUnit = new DLCheckUnitInstance();
            checkUnit.Init(inspectCfg);
        }
    }
}
